package com.annotation.configure;

import com.annotation.bean.Person;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

/**
 *  使用@PropertySource 读取外部文件保存到运行的环境变量中
 *
 *  在将外部配置文件读入到系统中有以下几种方案：
 *
 *
 * @PropertySource("配置文件")
 * @Value("${}")]
 * 实现  EmbeddedValueResolverAware  接口
 *
 */
@Configuration
@PropertySource(value = {"classpath:/bean.properties"},encoding = "utf-8")
public class TestConfigOfPropertyValues  implements EmbeddedValueResolverAware {

    StringValueResolver   resolver;

    @Bean
    public Person  person(){
        return new Person(1,resolver.resolveStringValue("${db.name}"));
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.resolver=resolver;
    }


}
